#!/usr/bin/env python
# encoding: utf-8
# Date: 2017-06-19 09:07:37
# file:views.py
# Email: wang521kang@outlook.com
# Author: 王康
import os
import uuid

from django.conf import settings

from commodity.models import Commodity, CommodityClassification, CommodityProperty, CommodityPropertyValue, CommodityImg
from utils.views import get_json, request_parms_check


@request_parms_check(commodity_name=('商品名称', 'r'), commodity_classification=('商品分类', 'r'),
                     commodity_price=('商品单价', 'r,int'), commodity_preferential=('商品优惠', ''))
def create_commodity(request, commodity_name, commodity_classification, commodity_price, commodity_preferential):
    obj = CommodityClassification.objects.get(classification=commodity_classification)
    commodity = Commodity()
    commodity.name = commodity_name
    commodity.price = commodity_price
    commodity.preferential = commodity_preferential
    commodity.commodity_classification = obj
    commodity.save()
    res = {}
    res['id'] = commodity.id
    return get_json(True, '创建成功', res)


@request_parms_check(commodity_property=('商品属性', 'r'), commodity_id=('商品id', 'r,int'))
def create_commodity_property(request, commodity_property, commodity_id):
    commodityproperty = CommodityProperty()
    commodityproperty.commodity_id = commodity_id
    commodityproperty.property = commodity_property
    commodityproperty.save()
    res = {}
    res['id'] = commodityproperty.id
    return get_json(True, '属性创建成功', res)


@request_parms_check(commodity_property_value=('商品属性的值', 'r'), commodity_property_id=('商品属性id', 'r,int'))
def create_commodity_property_value(request, commodity_property_value, commodity_property_id):
    commoditypropertyvalue = CommodityPropertyValue()
    commoditypropertyvalue.commodity_property_id = commodity_property_id
    commoditypropertyvalue.value = commodity_property_value
    commoditypropertyvalue.save()
    return get_json(True, '属性值添加成功')


@request_parms_check(commodity_classification=('商品分类', 'r'), )
def create_commodity_class(request, commodity_classification):
    commodity_class = CommodityClassification()
    commodity_class.classification = commodity_classification
    commodity_class.save()
    return get_json(True, '分类创建成功')


def query_commodity_class(request):
    l = []
    query = CommodityClassification.objects.all()
    for obj in query:
        res = {}
        res['id'] = obj.id
        res['classification'] = obj.classification
        l.append(res)
    return get_json(True, '查询所有分类', l)


def query_commodity(request):
    l = []
    query = Commodity.objects.all()
    for obj in query:
        res = {}
        res['id'] = obj.id
        res['name'] = obj.name
        res['price'] = obj.price
        res['preferential'] = obj.preferential
        res['commodity_classification'] = obj.commodity_classification.classification
        query_property = CommodityProperty.objects.filter(commodity_id=obj.id)
        for item in query_property:
            res['property'] = item.property
            query_property_value = CommodityPropertyValue.objects.filter(commodity_property_id=item.id)
            for object in query_property_value:
                res['value'] = object.value
        l.append(res)
    return get_json(True, '', l)


@request_parms_check(commodity_id=('商品id', 'r,int'))
def get_commodity(request, commodity_id):
    obj = Commodity.objects.get(id=commodity_id)
    res = {}
    res['id'] = obj.id
    res['name'] = obj.name
    res['price'] = obj.price
    res['preferential'] = obj.preferential
    query_commodity_img = CommodityImg.objects.filter(commodity_id=obj.id, is_active=True).all()
    li = []
    res['li'] = li
    for item2 in query_commodity_img:
        arg = {}
        arg['img_url'] = item2.img_url
        arg['img_id'] = item2.id
        li.append(arg)
    res['commodity_classification'] = obj.commodity_classification.classification
    query_property = CommodityProperty.objects.filter(commodity_id=obj.id).all()
    l = []
    res['p'] = l
    for item in query_property:
        arg1 = {}
        l.append(arg1)
        arg1['property'] = item.property
        arg1['property_id'] = item.id
        query_property_value = CommodityPropertyValue.objects.filter(commodity_property_id=item.id).all()
        l2 = []
        arg1['v'] = l2
        for object in query_property_value:
            arg2 = {}
            l2.append(arg2)
            arg2['value'] = object.value
            arg2['value_id'] = object.id
            arg2['property_id'] = item.id
    return get_json(True, '', res)


@request_parms_check(commodity_id=('商品id', 'r,int'), commodity_name=('商品名字', 'r'),
                     commodity_classification=('商品分类', 'r'), commodity_preferential=('商品优惠', ''),
                     commodity_price=('商品单价', 'r,int'))
def modify_commodity(request, commodity_id, commodity_name, commodity_classification, commodity_preferential,
                     commodity_price):
    get_com = Commodity.objects.filter(id=commodity_id).get()
    get_com.name = commodity_name
    obj = CommodityClassification.objects.get(classification=commodity_classification)
    get_com.commodity_classification = obj
    get_com.preferential = commodity_preferential
    get_com.price = commodity_price
    get_com.save()
    return get_json(True, '商品修改成功')


@request_parms_check(property_id=('属性id', 'r,int'), com_property=('商品属性', 'r'))
def modify_property(request, property_id, com_property):
    get_property = CommodityProperty.objects.get(id=property_id)
    get_property.property = com_property
    get_property.save()
    return get_json(True, '属性修改成功')


@request_parms_check(value_id=('属性id', 'r,int'), property_value=('商品属性值', 'r'))
def modify_property_value(request, value_id, property_value):
    get_property_value = CommodityPropertyValue.objects.get(id=value_id)
    get_property_value.value = property_value
    get_property_value.save()
    return get_json(True, '属性值修改成功')


@request_parms_check(price1=('价格1', 'int'), price2=('价格2', 'int'), com_classification=('商品分类', ''))
def query_commodity_search(request, price1, price2, com_classification):
    l = []
    query = Commodity.objects.filter(commodity_classification__classification=com_classification).all()
    if isinstance(price1, (int, float)):
        query = query.filter(price__gt=price1)
    if isinstance(price2, (int, float)):
        query = query.filter(price__lt=price2)
    for obj in query:
        res = {}
        res['id'] = obj.id
        res['name'] = obj.name
        res['price'] = obj.price
        res['preferential'] = obj.preferential
        res['commodity_classification'] = obj.commodity_classification.classification
        query_property = CommodityProperty.objects.filter(commodity_id=obj.id)
        for item in query_property:
            res['property'] = item.property
            query_property_value = CommodityPropertyValue.objects.filter(commodity_property_id=item.id)
            for object in query_property_value:
                res['value'] = object.value
        l.append(res)
    return get_json(True, '', l)


@request_parms_check(goods_id=('商品id', 'int'))
def create_img(request, goods_id):
    get_com = Commodity.objects.get(id=goods_id)
    img_file = request.FILES['img']  # 获取 表单中 img对应的file文件
    uri = '/media/pict/%s.jpg' % goods_id
    get_com.img_url = uri
    get_com.save()
    dest = os.path.join(settings.MEDIA_ROOT, 'pict', "%s.jpg" % goods_id)  # 拼接出文件存储的路径
    if os.path.exists(dest):
        os.remove(dest)
    with open(dest, "wb+") as destination:
        for chunk in img_file.chunks():
            destination.write(chunk)
    return get_json(True, '图片上传成功')


@request_parms_check(goods_id=('商品id', 'int'))
def create_imgs(request, goods_id):
    com_img = CommodityImg()
    img_file = request.FILES['img']  # 获取 表单中 img对应的file文件
    name = str(uuid.uuid4())
    uri = '/media/pict/%s.jpg' % name
    com_img.commodity_id = goods_id
    com_img.img_url = uri
    com_img.save()
    dest = os.path.join(settings.MEDIA_ROOT, 'pict', "%s.jpg" % name)  # 拼接出文件存储的路径
    if os.path.exists(dest):
        os.remove(dest)
    with open(dest, "wb+") as destination:
        for chunk in img_file.chunks():
            destination.write(chunk)
    return get_json(True, '图片上传成功')


@request_parms_check(img_id=('图片id', 'r,int'))
def delete_img(request, img_id):
    img = CommodityImg.objects.get(id=img_id)
    img.is_active = False
    img.save()
    return get_json(True, '')
